<?php
namespace Quiz\Controller;


use Think\Controller;


/**
 * 后台首页控制器
 */
class WippfController extends Controller{

    /**
     * 显示问卷
     * @param $user_id
     */

    public function show_wippf($user_id)
    {

        $M = M('calc_result');

        $desc = M("element_desc")->where(['quiz_id' => 7])->select();

        $data = $M->join('element_info on element_info.divisor_level = calc_result.divisor_level
        AND calc_result.element_index = element_info.element_index AND calc_result.quiz_id = element_info.quiz_id')->where(['calc_result.user_id ' => $user_id, 'calc_result.quiz_id' => 7])->select();

        $rs1 = [];
        $rs2 = [];
        $rs3 = [];
        $rs4 = [];
        $rs5 = [];
        $rs6 = [];
        $desc_k = 0;

        $element_index2 = ['α', 'γ', 'K', 'e', 'w', 'i'];
        $e_k = 0;

        foreach ($data as $key => $value){
            if ($key <= 10){
                $rs1[] = $value;

                foreach($rs1 as &$r){
                    for($k = 3;$k <= 12; $k ++){
                        $keyname = 'score_'.$k;
                        if ($r['row_score'] == $k)
                            $r['score_section'][$keyname] = '〇';
                        else
                            $r['score_section'][$keyname] = '';

                    }
                }


            }
            else if ($key >10 && $key <= 18){
                $rs2[] = $value;
                foreach($rs2 as &$r){
                    for($k = 3;$k <= 12; $k ++){
                        $keyname = 'score_'.$k;
                        if ($r['row_score'] == $k)
                            $r['score_section'][$keyname] = '〇';
                        else
                            $r['score_section'][$keyname] = '';

                    }
                }
            }
            else if ($key >18 && $key <= 22){
                $value['desc_level1'] = $desc[$desc_k++]['reg_desc'];
                $value['desc_level2'] = $desc[$desc_k++]['reg_desc'];
                $rs3[] = $value;

                foreach($rs3 as &$r){
                    for($k = 3;$k <= 12; $k ++){
                        $keyname = 'score_'.$k;
                        if ($r['row_score'] == $k)
                            $r['score_section'][$keyname] = '〇';
                        else
                            $r['score_section'][$keyname] = '';

                    }
                }
            }
            else if ($key >22 && $key <=28){
                $value['desc_level1'] = $desc[$desc_k++]['reg_desc'];
                $value['desc_level2'] = $desc[$desc_k++]['reg_desc'];
                $rs4[] = $value;

                foreach($rs4 as &$r){
                    for($k = 3;$k <= 12; $k ++){
                        $keyname = 'score_'.$k;
                        if ($r['row_score'] == $k)
                            $r['score_section'][$keyname] = '〇';
                        else
                            $r['score_section'][$keyname] = '';

                    }
                }
            }
            else if ($key >28 && $key <=31){
                $value['desc_level1'] = $desc[$desc_k++]['reg_desc'];
                $value['desc_level2'] = $desc[$desc_k++]['reg_desc'];
                $value['element_index'] = $element_index2[$e_k++];
                $rs5[] = $value;

                foreach($rs5 as &$r){
                    for($k = 11;$k <= 44; $k ++){
                        $keyname = 'score_'.$k;
                        if ($r['row_score'] == $k)
                            $r['score_section'][$keyname] = '〇';
                        else
                            $r['score_section'][$keyname] = '';

                    }
                }
            }
            else if ($key >31 && $key <=34){
                $value['desc_level1'] = $desc[$desc_k++]['reg_desc'];
                $value['desc_level2'] = $desc[$desc_k++]['reg_desc'];
                $value['element_index'] = $element_index2[$e_k++];
                $rs6[] = $value;

                foreach($rs6 as &$r){
                    for($k = 8;$k <= 32; $k ++){
                        $keyname = 'score_'.$k;
                        if ($r['row_score'] == $k)
                            $r['score_section'][$keyname] = '〇';
                        else
                            $r['score_section'][$keyname] = '';

                    }
                }

            }

        }

        $this->assign('wippf_resultTable0', $rs1);
        $this->assign('wippf_resultTable1', $rs2);
        $this->assign('wippf_resultTable2', $rs3);
        $this->assign('wippf_resultTable3', $rs4);

        $this->assign("wippf_resultTable_section2_0", $rs5);
        $this->assign("wippf_resultTable_section2_1", $rs6);

        $user_info = M("users")->where(['uid'=>$user_id])->find();
        $this->assign("userinfo", $user_info);
        $this->display("Quiz@Wippf:show_wippf");
    }


    /**
     * 显示团体报告
     * @param int $oid
     * @param string $quiztitle
     * @param int $display
     * @return array
     */


    public function show_wippf_groupreport($oid =0, $quiz_grouptitle = "")
    {

        if (!$oid && !$quiz_grouptitle){
            $uid_list = M()->query("select users.* from users, calc_result where users.uid = calc_result.user_id and users.oid = 0 GROUP BY users.uid;");
        }
        else{
            $uid_list = M('orgnizer_userquizlist')->field('uid')->where(['title' => $quiz_grouptitle , 'oid' => $oid])->select();
        }

        $tvalue_list = M('calc_result');
        $rs = [];
        $i = 0;

        foreach($uid_list as $uid){
            $data = $tvalue_list->where(['user_id' => $uid['uid'], 'quiz_id' => 7])->select();
            if (!$oid && !$quiz_grouptitle){

            }
            else{
                $userinfo = D('Orgnizeruserclass')->relation(true)->where(['uid' => $uid['uid']])->select();
                $userinfo = $userinfo[0];
            }


            foreach($data as &$d){
                if ($d['trans_score'] <=0 )
                    $d['trans_score'] = $d['row_score'];//没有转换分则显示原始分
            }

            if ($data){
                $rs[$i]['score_data'] = $data;
                if (!$oid && !$quiz_grouptitle){
                    $rs[$i]['username'] =$uid['username'];
                }
                else{
                    $rs[$i]['username'] =$userinfo['Users']['username'];
                }



                //$rs[$i]['class'] = $this->convertClassJson($userinfo['class']);
                $rs[$i]['class'] = convertClassJson($userinfo['class']);

                $i ++;
            }
        }

        $i = 0;
        $element_info = M("element_info")->where(["quiz_id" => 7])->select();
        $element_name = [];
        foreach($element_info as $e){
            $element_name[$i] = $e['element_name'];
            $i++;
        }

        //var_dump($rs[0]);
        //var_dump($element_name);
       //


        $this->assign("element_name", $element_name);
        $result_table = A("Quiz/Quiz")->WarnUserList($rs,$oid,"wippf_demo", $quiz_grouptitle);
        $this->assign('result_table', $result_table);
        $this->assign('title', $quiz_grouptitle);
        $this->display("Quiz@Wippf:show_wippf_groupreport");

//        if ($display == 1){
//            $this->assign("element_name", $element_name);
//            $this->assign('result_table', $relult_table);
//            $this->assign('title', $quiztitle);
//            $this->display();
//        }
//        else{
//            $rs = [];
//            $rs['result_table'] = $relult_table;
//            $rs['element_name'] = $element_name;
//            return $rs;
//        }
    }



    /**
     * 计算问卷
     * @param $uid
     * @throws \Exception
     */

    public function calcAnswer($uid)
    {
        $url = "http://localhost:8090/demo/Main";

        $data['action'] = "calc";
        $data['userid'] = $uid;
        $data['quiz_id'] = 7;

        $httpstr = http($url, $data, 'GET', array("Content-type: text/html; charset=utf-8"));//调用计算部分java

        if ($httpstr)
            return true;
        else
            return false;

        //var_dump($httpstr);
    }


    /**
     * 测试随机生成答案
     * @param $user_id
     */

    public function randomAnswer($user_id)
    {
        for($qst_id = 1; $qst_id <= 88; $qst_id++){

            $rs[] = [
              'quiz_id' => 7,
              'qst_id' => $qst_id,
                'selection' => rand(1,4),
                'user_id' => $user_id

            ];

        }


        //var_dump($rs);
        M('user_answer')->addAll($rs);
        if ($this->calcAnswer($user_id))
            echo "$user_id wippf random answer success";
        else
            echo "$user_id wippf random answer failed";

    }

    public function wippf_sample()
    {
        $this->show_wippf(34);
    }

}
